﻿Public Class Consultation_Screen

    Private _ctrl As Consultation_CTRL

    Private _flagAjout As Boolean = False
    Private _initEnCours As Boolean


#Region "Cycle de vie de la fenetre Consultation"

    Public Sub New(ByVal controleur As Consultation_CTRL)

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        _ctrl = controleur
    End Sub

    Private Sub Consultation_Screen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        _initEnCours = True

        CmbType.DataSource = _ctrl.listeTypeActe
        CmbType.Enabled = False
        CmbLibelle.Enabled = False

        With BarBoutonCONSULTATION

            .PnlGestionDonnées.Visible = True
            .BtnValider.Visible = True
            .BtnValider.Enabled = False

        End With

        With _ctrl.consultationCourante

            TxtDate.Text = Format(.DateConsultation, "dd/MM/yyyy")
            TxtVeto.Text = _ctrl.veterinaireCourant.NomVeto
            RtbCommentaire.Text = .Commentaire

        End With

        With _ctrl.animalCourant

            TxtCode.Text = .CodeAnimal.ToString
            TxtNom.Text = .NomAnimal
            txtEspece.Text = .espece
            TxtRace.Text = .race
            TxtCouleur.Text = .Couleur
            TxtSexe.Text = .Sexe
            TxtTatouage.Text = .Tatouage

        End With

        refreshDataGridView()
        refreshEcran()


        If _ctrl.consultationCourante.Etat > 0 Then
            RtbCommentaire.ReadOnly = True
            BarBoutonCONSULTATION.PnlGestionDonnées.Enabled = False
            BarBoutonCONSULTATION.PnlNavigation.Enabled = False
            BarBoutonCONSULTATION.PnlNavigationProgramme.Enabled = False


        End If

        _initEnCours = False

    End Sub

    Private Sub Consultation_Screen_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        _ctrl.Dispose()
    End Sub

#End Region

#Region "BarBouton"

    Private Sub BarBoutonCONSULTATION_Action(ByVal sender As Object, ByVal e As ActionEventArgs) Handles BarBoutonCONSULTATION.Action

        Select Case e.Action

            Case actionEnum.Ajouter
                _flagAjout = True
                BarBoutonCONSULTATION.BtnAjouter.Enabled = False
                DgvActes.Enabled = False
                effacerEcran()

            Case actionEnum.Enregistrer

                If _flagAjout = True AndAlso isSaisieOk() Then

                    _flagAjout = False
                    BarBoutonCONSULTATION.BtnAjouter.Enabled = True
                    _ctrl.sauverConsultation(CType(CmbLibelle.SelectedItem, Baremes), RtbCommentaire.Text, TXTprix.Text, TxtTatouage.Text)
                    BarBoutonCONSULTATION.BtnEnregistrer.Enabled = False
                    BarBoutonCONSULTATION.BtnValider.Enabled = True
                    CmbLibelle.Enabled = False
                    CmbType.Enabled = False

                    refreshDataGridView()

                ElseIf isSaisieOk() Then
                    _flagAjout = False
                    BarBoutonCONSULTATION.BtnAjouter.Enabled = True
                    BarBoutonCONSULTATION.BtnEnregistrer.Enabled = False
                    _ctrl.modifierConsultation(RtbCommentaire.Text)
                    CmbLibelle.Enabled = False
                    CmbType.Enabled = False
                    refreshDataGridView()

                End If

                DgvActes.Enabled = True

            Case actionEnum.Supprimer
                Dim confirmation As DialogResult
                confirmation = MessageBox.Show("Confirmez vous la suppression de cet acte ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                If confirmation = Windows.Forms.DialogResult.Yes Then
                    _ctrl.supprimerLigne(_ctrl.lignesConsultationCourante(_ctrl.index))
                    refreshDataGridView()
                End If

            Case actionEnum.Annuler

                Dim confirmation As DialogResult
                confirmation = MessageBox.Show("Confirmez vous l'annulation de la consultation ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                If confirmation = Windows.Forms.DialogResult.Yes Then
                    _flagAjout = False
                    _ctrl.supprimerConsultation()
                    RtbCommentaire.Text = ""
                    refreshDataGridView()
                End If

            Case actionEnum.Valider

                _ctrl.validerConsultation()
                MessageBox.Show("Consultation sauvegarder", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()

        End Select

    End Sub

#End Region


    Private Sub refreshDataGridView()

        If _ctrl.consultationCourante IsNot Nothing Then

            DgvActes.DataSource = Nothing
            DgvActes.DataSource = _ctrl.lignesConsultationCourante

        Else
            DgvActes.DataSource = Nothing
        End If

        If DgvActes.Rows.Count > 0 Then
            DgvActes.Rows(_ctrl.index).Selected = True
            refreshEcran()
        Else
            effacerEcran()
        End If


    End Sub

    Private Sub refreshEcran()

        _initEnCours = True

        TXTtotalConsultation.Text = _ctrl.totalConsultation
        TXTprix.ReadOnly = True
        TxtNbActes.Text = _ctrl.index + 1
        RtbCommentaire.Text = _ctrl.consultationCourante.Commentaire

        If DgvActes.Rows.Count > 0 Then

            BarBoutonCONSULTATION.BtnAnnuler.Enabled = True
            BarBoutonCONSULTATION.BtnSupprimer.Enabled = True
            BarBoutonCONSULTATION.BtnValider.Enabled = True

            CmbType.SelectedItem = _ctrl.lignesConsultationCourante(_ctrl.index).TypeActe
            CmbLibelle.SelectedItem = _ctrl.lignesConsultationCourante(_ctrl.index).Baremes


            TXTprix.Text = _ctrl.lignesConsultationCourante(_ctrl.index).Prix
        Else
            effacerEcran()
        End If

        _initEnCours = False

    End Sub

    Private Sub effacerEcran()

        CmbLibelle.DataSource = Nothing

        If _flagAjout = True Then
            CmbLibelle.Enabled = True
            CmbLibelle.SelectedIndex = -1
            CmbType.Enabled = True
            CmbType.SelectedIndex = -1
        Else
            CmbLibelle.Enabled = False
            CmbLibelle.SelectedIndex = -1
            CmbType.Enabled = False
            CmbType.SelectedIndex = -1
        End If

        TxtNbActes.Text = ""
        TxtPrix.Text = ""
        TxtMaxi.Text = ""
        TxtMini.Text = ""
        TXTtotalConsultation.Text = "0"

        BarBoutonCONSULTATION.BtnValider.Enabled = False
        BarBoutonCONSULTATION.BtnAnnuler.Enabled = False
        BarBoutonCONSULTATION.BtnSupprimer.Enabled = False

    End Sub

    Private Sub DgvActes_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DgvActes.SelectionChanged

        If DgvActes.SelectedRows.Count > 0 AndAlso _ctrl.index <> DgvActes.SelectedRows(0).Index AndAlso _ctrl.consultationCourante.LignesConsultations.Count > 1 Then
            _ctrl.index = DgvActes.SelectedRows(0).Index
            refreshEcran()
        End If

    End Sub

    Private Sub CmbType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbType.SelectedIndexChanged

        If CmbType.SelectedIndex >= 0 Then

            _ctrl.selectedBaremes = CmbType.SelectedItem

            CmbLibelle.DataSource = _ctrl.ListeLibelle

            CmbLibelle.SelectedIndex = 0

            If CmbType.SelectedItem = "TATO" And String.IsNullOrEmpty(_ctrl.animalCourant.Tatouage) Then
                TxtTatouage.ReadOnly = False
            Else
                TxtTatouage.ReadOnly = True
                TxtTatouage.BackColor = Color.White
            End If

        End If

    End Sub

    Private Sub CmbLibelle_Selectedindex(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbLibelle.SelectedIndexChanged

        If Not _initEnCours And _flagAjout = True Then

            If CmbLibelle.SelectedIndex >= 0 Then

                If CType(CmbLibelle.SelectedItem, Baremes).TarifFixe = 0 Then
                    TxtPrix.Text = ""
                    TxtMaxi.Text = CType(CmbLibelle.SelectedItem, Baremes).TarifMaxi
                    TxtMini.Text = CType(CmbLibelle.SelectedItem, Baremes).TarifMini
                    TXTprix.ReadOnly = False
                    TXTprix.BackColor = Color.White
                Else
                    TxtPrix.Text = CType(CmbLibelle.SelectedItem, Baremes).TarifFixe
                    TxtMaxi.Text = "-"
                    TxtMini.Text = "-"
                    TxtPrix.ReadOnly = True
                    TxtPrix.BackColor = Color.White
                End If

            End If

        End If

    End Sub

    Public Function isSaisieOk() As Boolean
        isSaisieOk = True
        Dim resultat As Decimal

        EPconsultation.SetError(TXTprix, "")

        If Not Decimal.TryParse(TXTprix.Text, resultat) Then
            isSaisieOk = False
            EPconsultation.SetError(TXTprix, "Mauvaise saisie")
        Else
            If TxtMaxi.Text <> "-" And TxtMini.Text <> "-" Then
                If TXTprix.Text > CType(CmbLibelle.SelectedItem, Baremes).TarifMaxi Then
                    isSaisieOk = False
                    EPconsultation.SetError(TXTprix, "Prix trop elevé")

                ElseIf TXTprix.Text < CType(CmbLibelle.SelectedItem, Baremes).TarifMini Then
                    isSaisieOk = False
                    EPconsultation.SetError(TXTprix, "Prix trop bas")
                End If
            End If
        End If

        If Not String.IsNullOrEmpty(_ctrl.animalCourant.Tatouage) And CmbType.Text = "TATO" Then
            isSaisieOk = False
            MessageBox.Show("Cet animal est déjà tatoué", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ElseIf String.IsNullOrEmpty(_ctrl.animalCourant.Tatouage) And CmbType.Text = "TATO" Then
            isSaisieOk = False
            MessageBox.Show("Veuillez entrer le tatouage realiser pendant cet acte", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        If CmbType.Text = "VACC" AndAlso CType(CmbLibelle.SelectedItem, Baremes).getVaccins.QuantiteStock = 0 Then
            isSaisieOk = False
            MessageBox.Show("Ce vaccin est en rupture de stock !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Function

    Private Sub TxtPrix_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTprix.TextChanged

        If Not _initEnCours And _flagAjout = True Then
            'Impossible d'enregistrer tant qu'un acte n'a pas ete choisi
            If CmbLibelle.SelectedIndex >= 0 Then
                BarBoutonCONSULTATION.BtnEnregistrer.Enabled = True
            End If

        End If


    End Sub

    Private Sub RtbCommentaire_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RtbCommentaire.TextChanged

        If Not _initEnCours Then
            'Impossible d'enregistrer tant qu'un acte n'a pas ete choisi
            If CmbLibelle.SelectedIndex >= 0 Then
                BarBoutonCONSULTATION.BtnEnregistrer.Enabled = True
            End If
        End If

    End Sub
End Class